<?php
class customer_dao extends abstract_user_dao implements interface_user_dao {

    public function __construct($registry) {
        parent::__construct($registry);
        $this->table_user = $this->db_prefix . "customer";
        $this->table_group = $this->db_prefix . "customer_group";
        $this->user_type = "customer";
    }

    public function login($email, $password) {

        if($customer = $this->get_by_email($email)) {
            if($customer->login($email, $password)) {
                return $customer;
            }
        }
        return false;
    }

    public function get_permissions(interface_user $user){
        return array();
    }

    public function update_session(interface_user $user, $session) {

        if ($user->getCart() && is_string($user->getCart())) {
            $cart = unserialize($user->getCart());

            foreach ($cart as $key => $value) {
                if (!array_key_exists($key, $session->data['cart'])) {
                    $session->data['cart'][$key] = $value;
                } else {
                    $session->data['cart'][$key] += $value;
                }
            }			
        }

        if ($user->getWishlist() && is_string($user->getWishlist())) {
            if (!isset($session->data['wishlist'])) {
                $session->data['wishlist'] = array();
            }

            $wishlist = unserialize($user->getWishlist());

            foreach ($wishlist as $product_id) {
                if (!in_array($product_id, $session->data['wishlist'])) {
                    $session->data['wishlist'][] = $product_id;
                }
            }			
        }
    }

    public function update_user(interface_user $user, $session){

        $update_query = $this->pdo->prepare("UPDATE " . $this->table_user . " SET 
            cart      = :cart,
            wishlist  = :wishlist  
            WHERE user_id = :user_id");

        $update_query->execute(array(
            ':cart' => (isset($session->data['cart']) ? serialize($session->data['cart']) : ''), 
            ':wishlist'  => (isset($session->data['wishlist']) ? serialize($session->data['wishlist']) : ''),
            ':user_id' => $user->get_id()
        ));
    }

    public function update_ip(interface_user $user, $ip) {

        parent::update_ip($user, $ip);        

        $ip_query = $this->pdo->prepare("SELECT * FROM " . $this->setting->get('db_prefix') . "customer_ip 
            WHERE user_id = :user_id 
            AND ip = :ip");

        $ip_query->execute(array(
            ':user_id'  => $user->get_id(),
            ':ip' => $ip
        ));

        if (!$ip_query->rowCount()) {
            $update_ip_query = $this->pdo->prepare("INSERT INTO " . $this->setting->get('db_prefix') . "customer_ip SET 
                user_id = :user_id, 
                ip          = :ip, 
                date_added = NOW()");

            $update_ip_query->execute(array(
                ':user_id'  => $user->get_id(), 
                ':ip' => $ip
            ));
        }
    }

    public function getBalance(interface_user $user) {

        $query_balance = $this->pdo->prepare("SELECT SUM(amount) AS total 
            FROM " . $this->setting->get('db_prefix') . "customer_transaction 
            WHERE user_id = :user_id");

        $query_balance->execute(array(
            ':user_id' => $user->get_id()
        ));

        $row = $query_balance->fetch(PDO::FETCH_ASSOC);

        return $row['total'];

    }

    public function getRewardPoints(interface_user $user) {

        $query_reward = $this->pdo->prepare("SELECT SUM(points) AS total 
            FROM " . $this->setting->get('db_prefix') . "customer_reward 
            WHERE user_id = :user_id");
        
        $query_reward->execute(array(
            ':user_id' => $user->get_id()
        ));

        $row = $query_reward->fetch(PDO::FETCH_ASSOC);

        return $row['total'];

    }

}
